
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
class advertising:
    def __init__(self):  # 进入页面
        wd=webdriver.Firefox()
        wd.maximize_window()
        host="http://192.168.0.118"
        wd.get(host+"/TinyShop/index.php?con=admin&act=login")
        wd.implicitly_wait(2)
        self.wd=wd

    def login(self,user,pwd): #后台登录
        wd = self.wd
        wd.find_element_by_name("name").clear()
        wd.find_element_by_name("name").click()
        wd.find_element_by_name("name").send_keys(user)
        wd.find_element_by_name("password").clear()
        wd.find_element_by_name("password").click()
        wd.find_element_by_name("password").send_keys(pwd)
        wd.find_element_by_name("verifyCode").clear()
        wd.find_element_by_name("verifyCode").click()
        wd.find_element_by_name("verifyCode").send_keys("aaaa")
        wd.find_element_by_css_selector("input.button").click()

    def new_advertising(self,ad_name,width,height,describe,type,url_site,jpg_name): #内容管理-广告管理-新增
        wd=self.wd
        time.sleep(0.5)
        wd.find_element_by_link_text("内容管理").click()
        wd.find_element_by_link_text("广告管理").click()
        wd.find_element_by_link_text("添加").click()
        wd.find_element_by_name("name").click()
        wd.find_element_by_name("name").send_keys(ad_name)
        wd.find_element_by_name("width").click()
        wd.find_element_by_name("width").send_keys(width)
        wd.find_element_by_name("height").click()
        wd.find_element_by_name("height").send_keys(height)
        time.sleep(0.5)
        wd.find_element_by_name("start_time").click()
        time.sleep(0.5)
        wd.switch_to.frame(wd.find_element_by_xpath("//iframe[@frameborder=\"0\"]")) #焦点切换
        time.sleep(0.5)
        wd.find_element_by_id("dpTodayInput").click()
        wd.switch_to.default_content()
        wd.find_element_by_name("end_time").click()
        time.sleep(0.5)
        wd.switch_to.frame(wd.find_element_by_xpath("//iframe[@frameborder=\"0\"]"))
        time.sleep(0.5)
        wd.find_element_by_id("dpTodayInput").click()
        wd.switch_to.default_content()
        wd.find_element_by_name("is_open").click()
        wd.find_element_by_name("description").click()
        wd.find_element_by_name("description").send_keys(describe)
        m=wd.find_element_by_id("ad_type")
        Select(m).select_by_visible_text(type)
        wd.find_element_by_css_selector("button.button.select_button").click()
        time.sleep(0.5)
        wd.switch_to.frame(wd.find_element_by_xpath("//iframe[@frameborder=\"0\"]"))
        time.sleep(0.5)
        # wd.find_element_by_name("upfile").send_keys(r"C:C:\Users\Public\Pictures\Sample Pictures\八仙花.jpg") #浏览
        wd.find_element_by_xpath("/html/body/div[2]/ul/li[2]").click()
        wd.find_element_by_xpath("/html/body/div[2]/div/div[2]/ul/li[1]/img").click()
        time.sleep(1)
        wd.find_element_by_css_selector("button.button").click()
        wd.switch_to.default_content()
        time.sleep(0.5)
        wd.find_element_by_name("url[]").click()
        wd.find_element_by_name("url[]").send_keys(url_site)
        wd.find_element_by_name("title[]").click()
        wd.find_element_by_name("title[]").send_keys(jpg_name)
        wd.find_element_by_css_selector("input.button").click()
        return wd
    def __del__(self):
        self.wd.quit()




    # def del_advertising(self): #内容管理-广告管理-删除
    #     wd= self.wd
    #     time.sleep(0.5)
    #     wd.find_element_by_link_text("内容管理").click()
    #     wd.find_element_by_link_text("广告管理").click()
    #     wd.find_element_by_xpath("//input[@value=\"34\"]").click()
    #     wd.find_elements_by_link_text("处理").









if __name__ == "__main__":
    ad = advertising()
    ad.login("admin","123456")
    ad.new_advertising("娃哈哈","100","80","wahahAD钙奶","悬浮","饮料")
   #ad.del_advertising()











